﻿using Common.Contract;
using Common.Contract.Customer.Request.PortfolioManagement;
using Common.Service.Command;
using Common.Util;
using Customer.Model.Interface;
using Customer.Model.Model;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Customer.Core.ServiceCommand.PortfolioManagement
{
    public class UpdateAccountPlanningFinanceServiceCommand : ProviderServiceCommand<IProviderFactory, UpdateDataAccountPlanningRequest, ResponseRecord>
    {
        private string Cif;
        private string Dao;
        public UpdateAccountPlanningFinanceServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(UpdateDataAccountPlanningRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.DataUpdate.IsNullOrEmpty())
            {
                throw new ArgumentException("Record must be set", "request");
            }
            if (string.IsNullOrEmpty(request.Cif))
            {
                throw new ArgumentException("Cif customer must be set", "request");
            }
            else
            {
                Cif = Request.Cif;
            }
            if (string.IsNullOrEmpty(request.DaoChange))
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.DaoChange;
            }
        }

        protected override bool OnExecute(ResponseRecord response)
        {
            try
            {
                bool isUpdate = true;
                AccountPlanningHistory lastChange = Providers.Portfolios.RetrieveLastChangeByCif(Cif, 2);
                if (lastChange == null || !lastChange.Value.Equals(Request.DataUpdate))
                {
                    var array = JArray.Parse(Request.DataUpdate);
                    List<AccountPlanningFinance> listRecord = new List<AccountPlanningFinance>();
                    int orderNum = 1;
                    foreach (var row in array)
                    {
                        var arrayItem = JArray.Parse(row.ToString().Trim('{', '}'));
                        int i = 0;
                        if (arrayItem.Count() == 16)
                        {
                            i = 1;
                        }
                        AccountPlanningFinance record = new AccountPlanningFinance
                        {
                            Name = arrayItem[i].ToString(),
                            LastYear = arrayItem[i + 1].ToString(),
                            Year = arrayItem[i + 2].ToString(),
                            Month1 = arrayItem[i + 3].ToString(),
                            Month2 = arrayItem[i + 4].ToString(),
                            Month3 = arrayItem[i + 5].ToString(),
                            Month4 = arrayItem[i + 6].ToString(),
                            Month5 = arrayItem[i + 7].ToString(),
                            Month6 = arrayItem[i + 8].ToString(),
                            Month7 = arrayItem[i + 9].ToString(),
                            Month8 = arrayItem[i + 10].ToString(),
                            Month9 = arrayItem[i + 11].ToString(),
                            Month10 = arrayItem[i + 12].ToString(),
                            Month11 = arrayItem[i + 13].ToString(),
                            Month12 = arrayItem[i + 14].ToString(),
                            Cif = Cif,
                            OrderNum = orderNum,
                        };
                        listRecord.Add(record);
                        orderNum = orderNum + 1;
                    }

                    AccountPlanningHistory obj = new AccountPlanningHistory
                    {
                        Value = Request.DataUpdate,
                        Type = 2,
                        ChangeDate = DateTime.Now,
                        DaoChange = Dao,
                        Cif = Cif,
                        Id = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds
                    };
                    Dictionary<string, AccountPlanningFinance> objectUpdate = listRecord.ToDictionary(x => x.OrderNum.ToString());
                    isUpdate = Providers.Portfolios.UpdateAccountPlanningFinance(objectUpdate, Cif) && Providers.Portfolios.SaveAccountPlanningHistory(obj);
                }

                return isUpdate;
            }
            catch (Exception e)
            {
                throw new ArgumentException(e.Message);
            }
        }
    }
}
